# Copyright 2019 The IREE Authors
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

# Tests of end-to-end IREE support for individual ops in the XLA HLO dialect.
# Each test file should have a name matching the corresponding XLA HLO op and test only the
# functionality of that op (though may make use of other ops where necessary). Tests should be
# written using the IREE Check framework and should always pass on the reference VMLA backend.
# See https://github.com/google/iree/blob/main/docs/developers/developing_iree/testing_guide.md#iree-core-end-to-end-tests.

load("//build_tools/bazel:enforce_glob.bzl", "enforce_glob")
load("//build_tools/bazel:iree_check_test.bzl", "iree_check_single_backend_test_suite")

package(
    default_visibility = ["//visibility:public"],
    features = ["layering_check"],
    licenses = ["notice"],  # Apache 2.0
)

iree_check_single_backend_test_suite(
    name = "check_cuda",
    srcs = enforce_glob(
        # keep sorted
        [
            "abs.mlir",
            "add.mlir",
            "batch_norm_inference.mlir",
            "broadcast.mlir",
            "broadcast_add.mlir",
            "broadcast_in_dim.mlir",
            "clamp.mlir",
            "compare.mlir",
            "concatenate.mlir",
            "constant.mlir",
            "convert.mlir",
            "convolution.mlir",
            "cosine.mlir",
            "divide.mlir",
            "dot.mlir",
            "dot_general.mlir",
            "dynamic_slice.mlir",
            "dynamic_update_slice.mlir",
            "exponential.mlir",
            "exponential_minus_one.mlir",
            "fft.mlir",
            "finite.mlir",
            "floor.mlir",
            "gather.mlir",
            "iota.mlir",
            "log.mlir",
            "log_plus_one.mlir",
            "maximum.mlir",
            "minimum.mlir",
            "multiply.mlir",
            "negate.mlir",
            "pad.mlir",
            "reduce.mlir",
            "reduce_window.mlir",
            "remainder.mlir",
            "reshape.mlir",
            "reverse.mlir",
            "rsqrt.mlir",
            "select.mlir",
            "sine.mlir",
            "slice.mlir",
            "sort.mlir",
            "sqrt.mlir",
            "subtract.mlir",
            "tanh.mlir",
            "torch_index_select.mlir",
            "transpose.mlir",
            "while.mlir",
        ],
        include = ["*.mlir"],
        exclude = [
            "round.mlir",
            "scatter.mlir",
        ],
    ),
    compiler_flags = ["-iree-input-type=mhlo"],
    driver = "cuda",
    tags = [
        # CUDA cuInit fails with sanitizer on.
        "noasan",
        "nomsan",
        "notsan",
        "noubsan",
        "requires-gpu-nvidia",
    ],
    target_backend = "cuda",
)

iree_check_single_backend_test_suite(
    name = "check_dylib-llvm-aot_dylib",
    srcs = enforce_glob(
        # keep sorted
        [
            "abs.mlir",
            "add.mlir",
            "batch_norm_inference.mlir",
            "broadcast.mlir",
            "broadcast_add.mlir",
            "broadcast_in_dim.mlir",
            "clamp.mlir",
            "compare.mlir",
            "concatenate.mlir",
            "constant.mlir",
            "convert.mlir",
            "convolution.mlir",
            "cosine.mlir",
            "divide.mlir",
            "dot.mlir",
            "dot_general.mlir",
            "dynamic_slice.mlir",
            "dynamic_update_slice.mlir",
            "exponential.mlir",
            "exponential_minus_one.mlir",
            "fft.mlir",
            "finite.mlir",
            "floor.mlir",
            "gather.mlir",
            "iota.mlir",
            "log.mlir",
            "log_plus_one.mlir",
            "maximum.mlir",
            "minimum.mlir",
            "multiply.mlir",
            "negate.mlir",
            "pad.mlir",
            "reduce.mlir",
            "reduce_window.mlir",
            "remainder.mlir",
            "reshape.mlir",
            "reverse.mlir",
            "rsqrt.mlir",
            "scatter.mlir",
            "select.mlir",
            "sine.mlir",
            "slice.mlir",
            "sort.mlir",
            "sqrt.mlir",
            "subtract.mlir",
            "tanh.mlir",
            "torch_index_select.mlir",
            "transpose.mlir",
            "while.mlir",
        ],
        include = ["*.mlir"],
        exclude = [
            "round.mlir",
        ],
    ),
    compiler_flags = ["-iree-input-type=mhlo"],
    driver = "dylib",
    target_backend = "dylib-llvm-aot",
)

iree_check_single_backend_test_suite(
    name = "check_vmvx_vmvx",
    srcs = enforce_glob(
        # keep sorted
        [
            "abs.mlir",
            "add.mlir",
            "batch_norm_inference.mlir",
            "broadcast.mlir",
            "broadcast_add.mlir",
            "broadcast_in_dim.mlir",
            "clamp.mlir",
            "compare.mlir",
            "concatenate.mlir",
            "constant.mlir",
            "convert.mlir",
            "convolution.mlir",
            "cosine.mlir",
            "divide.mlir",
            "dot.mlir",
            "dot_general.mlir",
            "dynamic_slice.mlir",
            "dynamic_update_slice.mlir",
            "exponential.mlir",
            "exponential_minus_one.mlir",
            "fft.mlir",
            "finite.mlir",
            "floor.mlir",
            "gather.mlir",
            "iota.mlir",
            "log.mlir",
            "log_plus_one.mlir",
            "maximum.mlir",
            "minimum.mlir",
            "multiply.mlir",
            "negate.mlir",
            "pad.mlir",
            "reduce.mlir",
            "reduce_window.mlir",
            "remainder.mlir",
            "reshape.mlir",
            "reverse.mlir",
            "rsqrt.mlir",
            "select.mlir",
            "sine.mlir",
            "slice.mlir",
            "sqrt.mlir",
            "subtract.mlir",
            "tanh.mlir",
            "torch_index_select.mlir",
            "transpose.mlir",
            "while.mlir",
        ],
        include = ["*.mlir"],
        exclude = [
            "round.mlir",
            "scatter.mlir",  # TODO(GH-6000): Enable the test.
            "sort.mlir",
        ],
    ),
    compiler_flags = ["-iree-input-type=mhlo"],
    driver = "vmvx",
    target_backend = "vmvx",
)

iree_check_single_backend_test_suite(
    name = "check_vulkan-spirv_vulkan",
    srcs = enforce_glob(
        # keep sorted
        [
            "abs.mlir",
            "add.mlir",
            "batch_norm_inference.mlir",
            "broadcast.mlir",
            "broadcast_add.mlir",
            "broadcast_in_dim.mlir",
            "clamp.mlir",
            "compare.mlir",
            "concatenate.mlir",
            "constant.mlir",
            "convert.mlir",
            "convolution.mlir",
            "cosine.mlir",
            "divide.mlir",
            "dot.mlir",
            "dot_general.mlir",
            "dynamic_slice.mlir",
            "dynamic_update_slice.mlir",
            "exponential.mlir",
            "exponential_minus_one.mlir",
            "finite.mlir",
            "floor.mlir",
            "gather.mlir",
            "iota.mlir",
            "log.mlir",
            "log_plus_one.mlir",
            "maximum.mlir",
            "minimum.mlir",
            "multiply.mlir",
            "negate.mlir",
            "pad.mlir",
            "reduce.mlir",
            "reduce_window.mlir",
            "remainder.mlir",
            "reshape.mlir",
            "reverse.mlir",
            "rsqrt.mlir",
            "select.mlir",
            "sine.mlir",
            "slice.mlir",
            "sqrt.mlir",
            "subtract.mlir",
            "tanh.mlir",
            "torch_index_select.mlir",
            "transpose.mlir",
            "while.mlir",
        ],
        include = ["*.mlir"],
        exclude = [
            "fft.mlir",  # TODO(GH-5444): Enable the test.
            "round.mlir",
            "scatter.mlir",  # TODO(GH-6001): Enable the test.
            "sort.mlir",
        ],
    ),
    compiler_flags = ["-iree-input-type=mhlo"],
    driver = "vulkan",
    target_backend = "vulkan-spirv",
)

test_suite(
    name = "check",
    tests = [
        ":check_dylib-llvm-aot_dylib",
        ":check_vmvx_vmvx",
        ":check_vulkan-spirv_vulkan",
    ],
)
